package com.zxm.sunshineaiary.sql.service;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.zxm.sunshineaiary.info.DiaryInfo;
import com.zxm.sunshineaiary.sql.db.DBadapter;

public class SqlService {

	private final String TAG = "SqlService";

	private static SqlService instance = null;

	private final String DB_YEAR_ID = "1";

	private DBadapter adapter = null;

	public static SqlService getInstance(Context c) {
		if (null == instance) {
			instance = new SqlService(c);
		}
		return instance;
	}

	public SqlService(Context c) {
		adapter = new DBadapter(c);
	}

	/**
	 * 根据年月获得day的数据
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public ArrayList<Integer> getDiaryDateDay(String year, String month) {
		Cursor c = null;
		ArrayList<Integer> list = null;
		try {
			adapter.open();
			StringBuffer sb = new StringBuffer();
			sb.append("'");
			sb.append(year);
			sb.append("-");
			sb.append(month);
			sb.append("-%");
			sb.append("'");

			Log.v(TAG, "String===" + sb.toString());
			c = adapter.getDiaryDate(sb.toString());
			if (null != c && c.moveToFirst()) {
				list = new ArrayList<Integer>();
				do {
					String s = c.getString(0);
					int day = Integer.parseInt(s.substring(8, 10));
					list.add(day);
				} while (c.moveToNext());
			}
		} finally {
			if (null != c) {
				c.close();
				adapter.closeDb();
			}
		}
		return list;
	}

	/**
	 * 根据日期得到Diary数据
	 * 
	 * @param date
	 * @return
	 */
	public DiaryInfo getDiary(String date) {
		Cursor c = null;
		DiaryInfo info = null;
		try {
			adapter.open();
			c = adapter.getDiary(date);
			if (null != c && c.moveToFirst()) {
				info = new DiaryInfo();
				do {
					info.setImageName(c.getString(0));
					info.setUrl(c.getString(1));
					info.setWeather(c.getString(2));
				} while (c.moveToNext());
			}
		} finally {
			c.close();
			adapter.closeDb();
		}
		return info;
	}

	/**
	 * 保存Diary数据
	 * 
	 * @param date
	 * @param info
	 */
	public void saveDiary(String date, DiaryInfo info) {
		Cursor c = null;
		try {
			Log.v(TAG, "date===" + date);
			adapter.open();
			c = adapter.getImageName(date);
			Log.v(TAG, "getColumnCount===" + c.getColumnCount());
			if (null != c && c.moveToFirst()) {
				Log.v(TAG, "if" + date);
				adapter.upDateImageName(info.imageName, date);
				adapter.upDateWeather(info.weather, date);
			} else {
				Log.v(TAG, "else===" + date);
				adapter.insertDiary(date, info.imageName, info.url,
						info.weather);
			}
		} finally {
			if (null != c) {
				c.close();
				adapter.closeDb();
			}
		}

	}

	/**
	 * 增加数据，先判断数据库中是否存在，如果不存在就加入家数据库
	 * 
	 * @param year
	 */
	public void insertSettingYear(String year) {
		Cursor cursor = null;
		boolean isRepeat = true;
		try {
			adapter.open();
			cursor = adapter.getSettingYearAll();
			if (null != cursor && cursor.moveToFirst()) {
				do {
					if (cursor.getString(0).equals(year)) {
						isRepeat = false;
					}
				} while (cursor.moveToNext());
			} else {
				adapter.insertSettingYear(year);
				isRepeat = false;
			}
			if (isRepeat) {
				adapter.insertSettingYear(year);
			}

		} finally {
			cursor.close();
			adapter.closeDb();
		}
	}

	/**
	 * 查询setting表的year列的全部数据
	 * 
	 * @return ArrayList<String>
	 */
	public ArrayList<String> getSettingYearAll() {
		ArrayList<String> al = new ArrayList<String>();
		Cursor c = null;
		try {
			adapter.open();
			c = adapter.getSettingYearAll();
			if (null != c && c.moveToFirst()) {
				do {
					al.add(c.getString(0));
				} while (c.moveToNext());
			}
		} finally {
			c.close();
			adapter.closeDb();

		}
		return al;
	}

	public boolean deleteDiary(String date) {
		boolean state = true;
		try {
			adapter.open();
			adapter.deleteDiary(date);
		} catch (Exception e) {
			state = false;
		} finally {
			adapter.closeDb();
		}
		return state;
	}
}
